package com.zuowei.ycy.user.ui.login;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.and.yzy.frame.util.AppManger;
import com.and.yzy.frame.util.MatchStingUtil;
import com.and.yzy.frame.util.RetrofitUtils;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.domain.UserInfo;
import com.zuowei.ycy.user.http.RegisterLog;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.ui.MainAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yzy on 2016/4/19.
 */
public class RegistAty extends BaseToolbarAty {
    @Bind(R.id.et_acount)
    EditText mEtAcount;
    @Bind(R.id.et_yanzhen)
    EditText mEtYanzhen;
    @Bind(R.id.tv_yanzhenma)
    TextView mTvYanzhenma;
    @Bind(R.id.et_pwd)
    EditText mEtPwd;
    @Bind(R.id.et_pwd_once)
    EditText mEtPwdOnce;

    MyCount mMyCount;

    @Override
    public int getLayoutId() {
        return R.layout.regist_layout;
    }

    @Override
    public void initData() {
        mToolbar.setTitle("注册");
    }

    @Override
    public void requestData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMyCount != null) {
            mMyCount.cancel();
        }
    }

    @OnClick({R.id.tv_commit, R.id.tv_yanzhenma})
    @Override
    public void btnClick(View view) {
        super.btnClick(view);
        switch (view.getId()) {
            case R.id.tv_commit:


                if (mEtAcount.getText().toString().equals("")) {
                    showToast("手机号不能为空!");
                    mEtAcount.requestFocus();
                    return;
                }
                if (!MatchStingUtil.isMobile(mEtAcount.getText().toString().trim())) {
                    showToast("手机号格式不正确!");
                    mEtAcount.requestFocus();
                    return;
                }

                if (mEtYanzhen.getText().toString().trim().length() < 4) {
                    showToast("验证码长度不正确");
                    mEtYanzhen.requestFocus();
                    return;
                }


                if (mEtPwd.getText().toString().trim().length() < 6) {
                    showToast("密码至少6位数");
                    mEtPwd.requestFocus();
                    return;
                }
                if (mEtPwdOnce.getText().toString().trim().length() < 6) {
                    showToast("确认密码至少6位数");
                    mEtPwdOnce.requestFocus();
                    return;
                }
                if (!mEtPwdOnce.getText().toString().trim().equals(mEtPwd.getText().toString().trim())) {
                    showToast("两次输入的密码不一致");
                    mEtPwdOnce.requestFocus();
                    return;
                }


                showLoadingDialog("注册中");

                doHttp(RetrofitUtils.createApi(RegisterLog.class).register(mEtAcount.getText().toString(), mEtPwd.getText().toString(), mEtYanzhen.getText().toString().trim()), 2);



                break;
            case R.id.tv_yanzhenma:
                //
                if (mEtAcount.getText().toString().equals("")) {
                    showToast("手机号不能为空!");
                    mEtAcount.requestFocus();
                    return;
                }
                if (!MatchStingUtil.isMobile(mEtAcount.getText().toString().trim())) {
                    showToast("手机号格式不正确!");
                    mEtAcount.requestFocus();
                    return;
                }


                showLoadingDialog("正在发送");

                doHttp(RetrofitUtils.createApi(RegisterLog.class).sendVerify(mEtAcount.getText().toString(), "activate"), 1);


                break;
        }
    }


    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {


        switch (what) {
            case 1:
                if (mMyCount == null) {
                    mMyCount = new MyCount(60000, 1000);
                }
                mMyCount.start();
                showToast("发送成功");
                break;
            case 2:
                showToast("注册成功");
                UserInfo userInfo= AppJsonUtil.getObject(result,UserInfo.class);
                UserManger.setUserInfo(userInfo);

                UserManger.setIsLogin(true);


                if (AppManger.getInstance().isAddActivity(MainAty.class)){

                   setResult(RESULT_OK);
                    finish();

                }else {
                    setHasAnimiation(false);
                    startActivity(MainAty.class,null);
                    finish();
                }


                break;

        }


        super.onSuccess(result, call, response, what);
    }

    private class MyCount extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mTvYanzhenma != null) {
                mTvYanzhenma.setEnabled(false);
                mTvYanzhenma.setText(millisUntilFinished / 1000 + "  s");
            }

        }

        @Override
        public void onFinish() {
            if (mTvYanzhenma != null) {
                mTvYanzhenma.setEnabled(true);
                mTvYanzhenma.setText("获取验证码");
            }

        }
    }
}
